When 2007 released, I researched and wrote an article about making SharePoint 2003 web parts with Visual Studio 2005, so why not write an article about how to use the awesomeness of Visual Studio 2010 to build a SharePoint 2007 web part. Now I know what you are saying, but Adam, that’s not complicated. Just upgrade my current projects and whala, I’m there. But are you tired of using WSPBuilder or some other custom methodology to create those Web Parts? I am, doesn’t Visual Studio have these 2010 SharePoint Project templates? Can I use one of them? oooooo – let’s find out.
First let’s get some prerequisites in place. You are going to need a development server will SharePoint Foundation or greater and VS2010. You’ll need this because when you create a project, VS2010 will look for an instance of SharePoint 2010 and when it doesn’t find it, errors galore. (I haven’t researched this to find out if you hobble something together to get around this. Know of way, share it with me and I will link to it from this blog.) You will also need to get your hands on the Microsoft.SharePoint.DLL from your v3 servers. (12/ISAPI)
Now that you have everything let’s get started.
First open VS2010 and choose to create a new project. Choose – Your Coding Language – SharePoint 2010 – Visual Web Part.
Now, give your project a name, location, etc… and click “OK”. Hit F5 after creation and make sure your web part is working. You want to do this because the next steps could cause your project not work if not done correctly. Wouldn’t do much for debugging if you weren’t absolutely certain your web part was working in the first place. Your web part should have been activated, so all you should have to do is edit your page, choose insert web part, and get your web part from the custom folder.
Is it working? If the answer is yes – great, if it is No, check the permission of your user account and the web.config file to make sure the safe control entry was written. If that doesn’t give you the answer, then I would start from scratch. If everything is working, continue.
Now that you have confirmed your Visual Web Part is working, let’s modify our project so that we can use this same process except, do so using a SharePoint v3 API.
First we will modify the SharePoint References we are using. Under the References folder, select the two Microsoft.SharePoint instances and delete them.
Now choose to add a reference, select the Browse tab, and locate your Microsoft.SharePoint.dll for v3 that you should have attained earlier.
Step one is now complete, time for step 2. Expand “Package” and double click on Package.package.
In the Properties Pane, Remove the “14.0” entry in the SharePoint Product Version Property Box.
Now let’s edit the Visual Web Part itself. Expand VisualWebPart1 or whatever the name of your web part is and double click on the UserControl.ascx.
We will need to alter 4 lines in the control.
First remove the line that says;
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Now in the three lines that say;
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Replace the “Version=14.0.0.0” with “Version=12.0.0.0”. The final result should look like;
Save your file and now hit F5 to build and test. Does it still work? If you did everything correctly it should. So let’s reflect on what we just did. We created a 2010 web part, converted it to 2007, are debugging it on 2010, and are now ready to try it on 2007, how awesome it that. Best of all, when creating the web part and debugging it you will only be doing so against v3 features so you won’t over extend your web part beyond on what 2007 can do. But now for the moment of truth.
Locate your Project directory where you are saving this project and enter the “bin/debug” folder. In there you should see the WSP created for your project. Copy the WSP to your 2007 server and add and deploy it then give it a try.
Don’t forget to activate your Web Part inside of Site Collection Features.
Now the question does come up, why build 2007 web parts anymore. No matter what anyone tells you, it will be at least 1.5 years before 50% or greater of the people running SharePoint out there will have 2010. Experience has shown us that so don’t hamstring the community by forcing the use of 2010 when people aren’t ready. Solve their solution now and know the work you are doing will work in both 2007 and 2010.
I hope this helps make your life a little bit easier.
ENJOY!